import {
  GraphQLID,
  GraphQLInt,
  GraphQLFloat,
  GraphQLString,
  GraphQLBoolean,
  GraphQLNonNull,
  GraphQLObjectType,
  GraphQLList
} from 'graphql';
import logger from '../../utils/logger';
import sleep from '../../utils/sleep';

// 定义接口返回的数据结构
const userType = new GraphQLObjectType({
  name: 'userItem',
  description: '用户信息',
  fields: () => ({
    id: {
      type: GraphQLID,
      description: '数据唯一标识'
    },
    username: {
      type: GraphQLString,
      description: '用户名'
    },
    age: {
      type: GraphQLInt,
      description: '年龄'
    },
    height: {
      type: GraphQLFloat,
      description: '身高'
    },
    isMarried: {
      type: GraphQLBoolean,
      description: '是否已婚',
      deprecationReason: '这个字段现在不需要了'
    }
  })
});

const userListType = new GraphQLObjectType({
  name: 'userList',
  fields: {
    pageSize: { type: GraphQLInt },
    pageNum: { type: GraphQLInt },
    total: { type: GraphQLInt },
    list: {
      type: new GraphQLList(userType),
      args: {
        id: { type: GraphQLID! }
      }
    }
  }
});

// 定义接口
export default {
  type: userListType,
  description: 'object类型数据例子',
  // 定义接口传参的数据结构
  args: {
    // id: {
    //   type: new GraphQLNonNull(GraphQLString),
    //   description: '是否有具体id'
    // }
  },
  resolve: async (root: any, params: any, context: any) => {
    await sleep(500);
    // 返回的数据与前面定义的返回数据结构一致
    return {
      pageSize: 10,
      pageNum: 1,
      total: 10,
      list: [
        {
          id: Math.floor(Math.random() * 10000),
          username: 'Davis1111',
          age: 23,
          height: 190.5,
          isMarried: true
        },
        {
          id: Math.floor(Math.random() * 10000),
          username: '2323',
          age: 3,
          height: 180,
          isMarried: false
        }
      ]
    };
  }
};
